/*----------------------------------------------------------------------
  - File name      : SCCB.c
  - Author         : Liu Chao
  - Update date    : 2020-07-14                   
  -	Copyright      : Gevico Electronic studio   
  - Module comments: 
-----------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
|                               INCLUDES                                |
-----------------------------------------------------------------------*/

#include "SCCB.h"
#include "SysTick.h"

/*-----------------------------------------------------------------------
|                                 DATA                                  |
-----------------------------------------------------------------------*/


/*-----------------------------------------------------------------------
|                               FUNCTION                                |
-----------------------------------------------------------------------*/


/*--------------------------------------------------------
| @Description: SCCB GPIO init function                  |
| @param      : None                                     |
| @return     : None                                     |
--------------------------------------------------------*/

void SCCB_GPIO_Config(void)
{
//--Struct declaration------------------------------------

    GPIO_InitTypeDef GPIO_InitStruct;

//--GPIO Configuration------------------------------------

    SCCB_GPIO_CLK_ENABLE();

    GPIO_InitStruct.Pin   = SCCB_SCL_PIN | SCCB_SDA_PIN;
    GPIO_InitStruct.Mode  = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull  = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; 
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
    SDA_H;
    SCL_H;
    SDA_OUT();

}

/*--------------------------------------------------------
| @Description: SCCB GPIO init function                  |
| @Param      : None                                     |
| @Return     : DISABLE / ENABLE                         |
| &Explain    : SDA ----______                           |
|               SCL ------____                           |
--------------------------------------------------------*/

static void SCCB_START(void)
{
    SDA_H;
    SCL_H;
    SysTick_Delay_us(50);
    SDA_L;
    SysTick_Delay_us(50);   
    SCL_L;
}

/*--------------------------------------------------------
| @Description: SCCB STOP function                       |
| @Param      : None                                     |
| @Return     : None                                     |
| &Explain    : SDA ____------                           |
|               SCL __--------                           |
--------------------------------------------------------*/

static void SCCB_STOP(void)
{
    SDA_L;
    SysTick_Delay_us(50); 
    SCL_H;
    SysTick_Delay_us(50); 
    SDA_H;
    SysTick_Delay_us(50); 
}

/*--------------------------------------------------------
| @Description: SCCB no ACK function                     |
| @Param      : None                                     |
| @Return     : None                                     |
| &Explain    : SDA _____---------                       |
|               SCL _______--_____                       |
--------------------------------------------------------*/

static void SCCB_NACK()
{
	SysTick_Delay_us(50); 
	SDA_H;
    SCL_H;
	SysTick_Delay_us(50); 
	SCL_L;
	SysTick_Delay_us(50); 
	SDA_L;
	SysTick_Delay_us(50); 
}


/*--------------------------------------------------------
| @Description: SCCB send byte function                  |
| @Param      : uint8_t Datne                            |
| @Return     : 0,success / 1,erro                       |
| &Explain    : None                                     |
--------------------------------------------------------*/

uint8_t SCCB_Send_Byte(uint8_t Dat)
{
    uint8_t i,Res;

    for(i=0; i<8;i++)
    {
        if(Dat & 0x80) SDA_H;
        else           SDA_L;
        Dat <<=1;
        SysTick_Delay_us(50);
        SCL_H;
        SysTick_Delay_us(50);
        SCL_L;
    }
    
    SDA_IN();
    SysTick_Delay_us(50);
    
    SCL_H;
    SysTick_Delay_us(50);
    
    if(SDA_READ) Res = 1; //Errp
    else         Res = 0; //Success

    SCL_L;
    SDA_OUT();
    return Res;
}

/*--------------------------------------------------------
| @Description: SCCB Receive byte function               |
| @Param      : None                                     |
| @Return     : Receive byte                             |
| &Explain    : None                                     |
--------------------------------------------------------*/

uint8_t SCCB_Receive_Byte(void)
{
    uint8_t i;
    uint8_t Receive_Byte = 0;
    
    SDA_IN();
    for(i = 8; i>0; i--)
    {
        SysTick_Delay_us(50);
        SCL_H;
        Receive_Byte <<= 1;
        if(SDA_READ) Receive_Byte++;
        SysTick_Delay_us(50);
        SCL_L;
    }
    SDA_OUT();
    return Receive_Byte;
}

/*--------------------------------------------------------
| @Description: SCCB Write Reg function                  |
| @Param      : uint16_t Address ,uint8_t Dat            |
| @Return     : 0 - success / 1 - erro                   |
| &Explain    :                                          |
--------------------------------------------------------*/

uint8_t SCCB_Write_Reg(uint16_t Address ,uint8_t Dat)
{
    uint8_t Res = 0;
    SCCB_START();
    if(SCCB_Send_Byte(DEV_ADR)) Res = 1;
    SysTick_Delay_us(100);
    if(SCCB_Send_Byte(Address)) Res = 1;
     SysTick_Delay_us(100);
    if(SCCB_Send_Byte(Dat))     Res = 1;   
    SCCB_STOP();
    return Res;
}

/*--------------------------------------------------------
| @Description: SCCB Read Reg function                   |
| @Param      : uint8_t * pBuffer, uint16_t length,      |
|               uint8_t Address                          |
| @Return     : ENABLE - success / DISABLE - erro        |
| &Explain    :                                          |
--------------------------------------------------------*/

uint8_t SCCB_Read_Reg(uint8_t Reg)
{
    uint8_t  Receive_Data = 0;
    SCCB_START();
    SCCB_Send_Byte(DEV_ADR);
    SysTick_Delay_us(100);
    SCCB_Send_Byte(Reg);
    SysTick_Delay_us(100);       
    SCCB_STOP();
    SysTick_Delay_us(100);

    SCCB_START();
    SCCB_Send_Byte(DEV_ADR | 0x01);
    SysTick_Delay_us(100);       
    Receive_Data = SCCB_Receive_Byte();
    SCCB_NACK();
    SCCB_STOP();

    return Receive_Data;
}


/*-----------------------------------------------------------------------
|          END OF FLIE        (C) COPYRIGHT Gevico Electronics          | 
-----------------------------------------------------------------------*/
